package servlet;

import bean.CateInfo;
import dao.CateDao;
import dao.impl.CateDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GoodServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method=req.getParameter("catemethod");
        CateInfo cateInfo=new CateInfo();
        CateDao cateDao=new CateDaoImpl();
        if ("edit".equals(method)){
            String cateid=req.getParameter("cateid");
            String cateName=req.getParameter("cateName");
            Integer id=Integer.parseInt(cateid);
            cateInfo.setId(id);
            cateInfo.setCateName(cateName);
            cateDao.update(cateInfo);
            resp.sendRedirect("cate");

        }

        if ("del".equals(method)){
            String cateid=req.getParameter("cateid");
            String cateName=req.getParameter("cateName");
            Integer id=Integer.parseInt(cateid);
            cateInfo.setId(id);
            cateInfo.setCateName(cateName);
            cateDao.delete(id);
            resp.sendRedirect("cate");
        }

        if ("addBig".equals(method)){
            Integer parentId=0;
            String des=req.getParameter("des");
            cateInfo.setParentId(parentId);
            String cateName=req.getParameter("cateName");
            cateInfo.setCateName(cateName);
            cateInfo.setDes(des);
            cateDao.bigadd(cateInfo);
            resp.sendRedirect("cate");
        }
        if ("addSmall".equals(method)){
            String parentid=req.getParameter("parentId");
            Integer parentId=Integer.parseInt(parentid);
            String cateName=req.getParameter("cateName");
            String des=req.getParameter("des");
            cateInfo.setParentId(parentId);
            cateInfo.setCateName(cateName);
            cateInfo.setDes(des);
            cateDao.bigadd(cateInfo);
            resp.sendRedirect("cate");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
